//
//  XQPopupViewController.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/21.
//

#import "XQPopupViewController.h"

@interface XQPopupViewController ()<UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning>

@property (nonatomic, assign) BOOL isPresented;

@end

@implementation XQPopupViewController

- (instancetype)init {
    if (self = [super init]) {
        self.modalPresentationStyle = UIModalPresentationCustom;
        self.transitioningDelegate = self;
    }
    return self;
}

- (void)showInViewController:(UIViewController *)vc completion:(void (^ __nullable)(void))completion {
    if (self.showType == XQPopupShowTypePresent) {
        [vc presentViewController:self
                         animated:YES
                       completion:completion];
        
    } else {
        [self willMoveToParentViewController:vc];
        [vc addChildViewController:self];
        [vc.view addSubview:self.view];
        [self didMoveToParentViewController:vc];
        
        [self.animView showInView:self.view completion:completion];
    }
}

- (void)hideAndCompletion:(void(^)(void))aCompletionBlock {
    if (self.showType == XQPopupShowTypePresent) {
        [self dismissViewControllerAnimated:YES
                                 completion:aCompletionBlock];
        
    } else {
        [self.animView hideAndCompletion:^{
            [self willMoveToParentViewController:nil];
            [self.view removeFromSuperview];
            [self removeFromParentViewController];
            [self didMoveToParentViewController:nil];
            
            if (aCompletionBlock) {
                aCompletionBlock();
            }
        }];
    }
}

#pragma mark - UIViewControllerTransitioningDelegate
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    if (self.showType == XQPopupShowTypePresent) {
        self.isPresented = YES;
        return self;
    }
    return nil;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    if (self.showType == XQPopupShowTypePresent) {
        self.isPresented = NO;
        return self;
    }
    return nil;
}

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.3f;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    if (self.isPresented) {
        [[transitionContext containerView] addSubview:[transitionContext viewForKey:UITransitionContextToViewKey]];
        
        [self.animView showInView:self.view completion:^{
            BOOL wasCancelled = [transitionContext transitionWasCancelled];
            [transitionContext completeTransition:!wasCancelled];
        }];
    } else {
        [[transitionContext containerView] addSubview:[transitionContext viewForKey:UITransitionContextFromViewKey]];
        
        [self.animView hideAndCompletion:^{
            BOOL wasCancelled = [transitionContext transitionWasCancelled];
            [transitionContext completeTransition:!wasCancelled];
        }];
    }
}

#pragma mark - Getter
- (XQPopupAnimView *)animView {
    if (!_animView) {
        _animView = [[XQPopupAnimView alloc] initWithAnimationType:XQPopupAnimationTop customView:self.customView];
        _animView.duration = [self transitionDuration:nil];
        @weakify(self);
        _animView.makeCustomConstraints = ^(XQPopupAnimView *animView, UIView *customView) {
            [customView mas_remakeConstraints:^(MASConstraintMaker *make) {
                if (animView.animationType == XQPopupAnimationCenter) {
                    (void)make.center;
                } else {
                    make.left.right.offset(0.f);
                    if (animView.animationType == XQPopupAnimationTop) {
                        make.top.offset(0.f);
                    } else {
                        make.bottom.offset(0.f);
                    }
                }
                if (animView.customHeight != 0) {
                    make.height.mas_equalTo(animView.customHeight);
                }
            }];
        };
        _animView.clickHandle = ^{
            @strongify(self);
            [self hideAndCompletion:nil];
        };
    }
    return _animView;
}

- (UIView *)customView {
    if (!_customView) {
        _customView = [[UIView alloc] init];
    }
    return _customView;
}



@end
